package com.lrkj.web.phone;

import com.lrkj.common.annotation.Log;
import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.core.domain.entity.RepairProject;
import com.lrkj.common.core.domain.entity.SysUser;
import com.lrkj.common.core.page.TableDataInfo;
import com.lrkj.common.enums.BusinessType;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.property.domain.Room;
import com.lrkj.waitress.domain.CustomInfo;
import com.lrkj.workorder.domain.WorkOrder;
import com.lrkj.workorder.domain.WorkOrderType;
import com.lrkj.workorder.service.IRepairProjectService;
import com.lrkj.workorder.service.IWorkOrderService;
import com.lrkj.workorder.service.IWorkOrderTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "工单管理")
@RequestMapping("/phone/workOrder/")
public class PhoneWorkOrderController extends CommonUseBaseController {
    @Autowired
    private IWorkOrderTypeService workOrderTypeService;

    @Autowired
    private IRepairProjectService repairProjectService;

    @Autowired
    private IWorkOrderService workOrderService;

    /**
     * 查询工单类型列表
     */
    @ApiOperation(value = "查询工单类型列表", notes = "查询工单类型列表")
    @GetMapping("workOrderType/list")
    public TableDataInfo list(WorkOrderType workOrderType) {
        startPage();
        this.changeParameter(workOrderType.getParams());
        List<WorkOrderType> list = workOrderTypeService.selectWorkOrderTypeList(workOrderType);
        return getDataTable(list);
    }


    /**
     * 获取报修项目下拉树列表
     */
    @GetMapping("workOrderType/treeselect")
    public AjaxResult treeselect(RepairProject repairProject) {
        List<RepairProject> repairProjects = repairProjectService.selectRepairProjectList(repairProject);
        return AjaxResult.success(repairProjectService.buildDeptTreeSelect(repairProjects));
    }


    /**
     * @param workOrder
     * @return
     */
    @GetMapping("list")
    public TableDataInfo selWorkOrderAll(WorkOrder workOrder) {
        TableDataInfo tableDataInfo;
        Room room = getRoom(workOrder.getRoomId());
        if (room == null) {
            tableDataInfo = getDataTable(null);
            tableDataInfo.setMsg("房间信息不存在！");
            return tableDataInfo;
        }

        //获取客户信息
        CustomInfo customInfo = getCustomInfo(room.getCommunityId());
        if (customInfo == null) {
            tableDataInfo = getDataTable(null);
            tableDataInfo.setMsg("未绑定房屋信息！请先绑定");
            return tableDataInfo;
        }

        workOrder.setOrderType(getOrderTypeId(workOrder.getOrderTypeCode()));
        workOrder.setReflectPeopleId(customInfo.getCustomId());
        startPage();
        List<WorkOrder> list = workOrderService.selWorkOrderAll(workOrder);
        return getDataTable(list);
    }


    /**
     * 物业端新增工单管理
     */
    @ApiOperation(value = "新增工单管理", notes = "新增工单管理")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:add')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping("/OrderAdd")
    public AjaxResult OrderAdd(@RequestBody WorkOrder workOrder)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        workOrder.setCharterMemberId(user.getUserId() + "");
        workOrder.setCharterMemberName(user.getNickName());
        return toAjax(workOrderService.insertWorkOrder(workOrder));
    }


    /**
     * 客户端新增工单管理
     */
    @ApiOperation(value = "新增工单管理", notes = "新增工单管理")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@Valid @RequestBody WorkOrder workOrder) {
        Room room = getRoom(workOrder.getRoomId());
        if (room == null) {
            return AjaxResult.error("房间信息不存在！");
        }

        //获取客户信息
        CustomInfo customInfo = getCustomInfo(room.getCommunityId());
        if (customInfo == null) {
            return AjaxResult.error("未绑定房屋信息！请先绑定");
        }
        workOrder.setOrderType(getOrderTypeId(workOrder.getOrderTypeCode()));
        workOrder.setCommunityId(room.getCommunityId());
        workOrder.setFloorId(room.getFloorId());
        workOrder.setUnitId(room.getUnitId());
        workOrder.setReflectPeopleId(customInfo.getCustomId());
        workOrder.setReflectPeople(customInfo.getCustomName());
        workOrder.setCharterMemberId(customInfo.getCustomId());
        workOrder.setCharterMemberName(customInfo.getCustomName());
        workOrder.setCharterTime(DateUtils.getNowDate());
        return toAjax(workOrderService.insertWorkOrder(workOrder));
    }


    public String getOrderTypeId(String orderTypeCode){
        WorkOrderType workOrderType=getWorkOrderTypes(orderTypeCode);
        if(workOrderType !=null){
            return workOrderType.getId();
        }
        return "";
    }

}
